// ignore_for_file: prefer_const_constructors
import 'dart:async';
import 'package:flutter/material.dart';

// import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';
import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';

class QGSendCodeDialog1 extends StatefulWidget {
  void Function()? toConfirm;
  String? url;
  int? ttype;
  dynamic data;
  QGSendCodeDialog1({super.key, this.toConfirm, this.url,this.data,this.ttype});

  @override
  State<QGSendCodeDialog1> createState() => _QGSendCodeDialogState();
}

class _QGSendCodeDialogState extends State<QGSendCodeDialog1> {
  final codeController = TextEditingController();
  final codeController1 = TextEditingController();
  Timer? timer;
  int countDown = 0;
  dynamic data;
  dynamic ttype
;
  
  @override
  void initState() {
    super.initState();
    data = widget.data;
    ttype = widget.ttype;

  }

  @override
  Widget build(BuildContext context) {
    return 
    AnimatedContainer(
        height: 340.rpx,
        duration: Duration(milliseconds: 300),
        padding: EdgeInsets.only(bottom: kSafeBottom),
        alignment: Alignment.bottomCenter,
        child: Stack(
          children: [
            Container(
              padding: EdgeInsets.fromLTRB(20.rpx, 25.rpx, 20.rpx, 0.rpx),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(20.rpx),
                    topRight: Radius.circular(20.rpx)),
              ),
              child: Column(children: [
                buildTitle(),
                SizedBox(height: 24.rpx),
                buildCode(),
                SizedBox(height: 30.rpx),
                buildButton(),
              ]),
            ),
            Positioned(
                right: 15.rpx,
                top: 15.rpx,
                width: 15.rpx,
                height: 15.rpx,
                child: GestureDetector(
                  onTap: () {
                    Navigator.of(context).pop();
                    timer = Timer.periodic(Duration(seconds: 1), (timer) {
                      timer.cancel();
                    });
                  },
                  child: QGImage(asset: "mine_nickname_close"),
                ))
          ],
        ));
  }

  buildTitle() {
    return Container(
      child: Text(
        ttype == 2?'求购':'委托',
        style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 16.rpx,
            color: Color(0xFFffffff)),
      ),
    );
  }

  buildCode() {
    return Column(
      children: [
        Row(
          children: [
      Expanded(
            child: Container(
          height: 55.rpx,
          padding: EdgeInsets.fromLTRB(15.rpx, 15.rpx, 15.rpx, 0.rpx),
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(
                color: Color(0XFF3D4854)
              )
            ),
              borderRadius: BorderRadius.circular(6.rpx),
              color: Color(0xFF232A32)),
          child: Column(
            children: [
              Expanded(
                  child: TextField(
                    textAlign:TextAlign.center,
                controller: codeController,
                cursorColor: Color(0xFFffffff),
                keyboardType: TextInputType.number,
                style: TextStyle(
                    fontWeight: FontWeight.normal,
                    fontSize: 14.rpx,
                    color: Color(0xFFffffff)),
                decoration: InputDecoration(
                    hintText: '请输入数量',
                    hintStyle: TextStyle(color: Color(0xFF999999)),
                    isCollapsed: true,
                    border: OutlineInputBorder(borderSide: BorderSide.none),
                    enabledBorder: InputBorder.none,
                    focusedBorder: InputBorder.none),
              )),
            ],
          ),
        ))
          ],
        )
       ,

       Row(
        children: [
        Expanded(
            child: Container(
          height: 55.rpx,
          padding: EdgeInsets.fromLTRB(15.rpx, 15.rpx, 15.rpx, 0.rpx),
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(
                color: Color(0XFF3D4854)
              )
            ),
              borderRadius: BorderRadius.circular(6.rpx),
              color: Color(0xFF232A32)),
          child: Column(
            children: [
              Expanded(
                  child: TextField(
                    textAlign:TextAlign.center,
                controller: codeController1,
                cursorColor: Color(0xFFffffff),
                keyboardType: TextInputType.number,
                style: TextStyle(
                    fontWeight: FontWeight.normal,
                    fontSize: 14.rpx,
                    color: Color(0xFFffffff)),
                decoration: InputDecoration(
                    hintText:ttype ==2? '最低求购价格':'委托价格',
                    hintStyle: TextStyle(color: Color(0xFF999999)),
                    isCollapsed: true,
                    border: OutlineInputBorder(borderSide: BorderSide.none),
                    enabledBorder: InputBorder.none,
                    focusedBorder: InputBorder.none),
              )),
            ],
          ),
        ))
        ],
       )
      
        
      ],
    );
  }

  buildButton() {
    return Row(
      children: [
        Expanded(
            child: Container(
          height: 44.rpx,
          decoration: BoxDecoration(
            gradient: const LinearGradient(
              begin: Alignment.centerLeft,
              end: Alignment.centerRight,
              colors: [Color(0xFF4AE3FF), Color(0xFF44FFF0)],
            ),
            borderRadius: BorderRadius.circular(19.rpx),
          ),
          child: TextButton(
            onPressed: () {
              toConfirmButton();
            },
            style: ButtonStyle(tapTargetSize: MaterialTapTargetSize.shrinkWrap),
            child: Text(
              '确认',
              style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 14.rpx,
                  color: Color(0xFF111111)),
            ),
          ),
        ))
      ],
    );
  }

  toSend() {
    if (countDown > 0) {
      return;
    }
    Navigator.of(context)
        .pushNamed(PageVerifyAli, arguments: User.current!.phone)
        .then((value) {
      if (value is! Map) return;
      start();
    });
  }

  toConfirmButton() async {
    var code = codeController.text;
    var code1 = codeController1.text;
    if (code.isEmpty) {
      toast('请输入数量');
      return;
    }
    if (code1.isEmpty) {
      toast('请输入价格');
      return;
    }
    unfocus(context);

     var param = {};
      param['pid'] = data?["pid"];
      param['num'] = code;
      param['price'] = code1;
      
Map<String, dynamic> stringMap = param.cast<String, dynamic>();
 dynamic uurlk
 ;
      if(ttype==2){
        uurlk = url_ask_addOrder;
      }else{
        uurlk = url_entrust_addOrder;

      }
    


    Network.post(uurlk, data: stringMap, context: context)
        .then((res) async {
      if (!res.success) {
        toast(res.msg);
        return;
      }

      var url = res.data["pay_url"];
      if (url is String && !url.startsWith("http")) {
        url = url;
      }
      if (url is! String || !url.startsWith("http")) {
        toast("数据解析失败");
        return;
      }

      // Navigator.of(context)
      //     .pushReplacementNamed(PageQGPayWebView, arguments: url);
          Navigator.of(context)
          .pushNamed(PageQGPayWebView, arguments: url);
    });

  }

  start() {
    countDown = 60;
    setState(() {});
    timer = Timer.periodic(Duration(seconds: 1), (timer) {
      countDown--;
      if (countDown == 0) {
        timer.cancel();
      }
      setState(() {});
    });
  }
}
